package com.boss.thirteen.controller;


import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.WfActionDTO;
import com.boss.common.enums.ActionTypeEnum;
import com.boss.common.enums.StatusCodeEnum;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.thirteen.dao.IThirteenAgencyVerifyDao;
import com.boss.thirteen.entity.ThirteenAgencyVerify;
import com.boss.thirteen.service.ISalaryThirteenWorkFlowService;
import com.boss.thirteen.service.IThirteenMouldStartAsyncService;
import com.boss.thirteen.vo.ThirteenAgencyVerifyVO;
import com.boss.thirteen.vo.ThirteenMouldVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作流统一接口类
 */
@RestController
@RequestMapping(value = "/salaryThirteenWorkFlowController", produces = MediaType.APPLICATION_JSON_VALUE)
public class SalaryThirteenWorkFlowController extends BasicinfoBaseController {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryThirteenWorkFlowController.class);

    @Autowired
    private ISalaryThirteenWorkFlowService iSalaryWorkFlowService;
    @Autowired
    private IThirteenMouldStartAsyncService thirteenMouldStartAsyncService;
    @Autowired
    private IThirteenAgencyVerifyDao thirteenAgencyVerifyDao;


    /**
     * 单位审核获取说明
     * @param tokenid
     * @param agencyId
     * @return
     */
    @GetMapping("/getAuditInstruction")
    public ApiResultDTO getAuditInstruction( @RequestHeader("X-Token") String tokenid,
                                             @RequestParam(value = "agencyId") String agencyId){
        try {
            UserDTO userDto = getUser(tokenid);
            WfActionDTO wfActionDTO =new WfActionDTO();
            List<Map> auditInstruction = iSalaryWorkFlowService.getAuditInstruction(userDto, wfActionDTO, agencyId,tokenid);
            return ApiResultDTO.success(auditInstruction);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询异常");
        }
    }

    @GetMapping("/getThirteenAgencyWorkStatus")
    public ApiResultDTO getThirteenAgencyWorkStatus( @RequestHeader("X-Token") String tokenid,
                                             @RequestParam(value = "menuId") String menuId,
                                             @RequestParam(value = "agencyId") String agencyId){
        try {
            UserDTO userDto = getUser(tokenid);
            WfActionDTO wfActionDTO =new WfActionDTO();
            wfActionDTO.setMenuguid(menuId);
            List<String> statusList = new ArrayList<>();
            statusList.add(StatusCodeEnum.TODO_BESENDBACK.getCode());//判断已审核流程
            List<String> bizkeyList = new ArrayList<>();
            ThirteenAgencyVerify agencyVerify = thirteenAgencyVerifyDao.getThirteenInfoByAgencyId(agencyId);
            bizkeyList.add(agencyVerify.getBizKey());
            List<Map> maps = iSalaryWorkFlowService.queryWorkflowInfoStatus(userDto, wfActionDTO, statusList, bizkeyList,tokenid);
            Map<String,Object> requestMap=new HashMap<>();
            requestMap.put("bizKey",agencyVerify.getBizKey());
            requestMap.put("isSalarySubmit",agencyVerify.getIsSalarySubmit());
            for (Map map : maps) {
                if (StringUtils.isEmpty(map.get("status"))) {
                    requestMap.put("status",1);
                    return ApiResultDTO.success(requestMap);//非待办状态
                }
            }
            requestMap.put("status",0);
            return ApiResultDTO.success(requestMap);//待办
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询异常");
        }
    }

    /**
     * 十三月工资初始流程审核
     * @param tv
     * @return
     */
    @PostMapping("/startDoWorkFlow")
    public ApiResultDTO startDoWorkFlow(@RequestHeader("X-Token") String tokenid,
                                        @RequestBody ThirteenMouldVO tv) {
        try {
            UserDTO userDTO = getUser(tokenid);
            thirteenMouldStartAsyncService.greateThitteenMonth(userDTO,tv,tokenid);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("启动失败", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }

    @PostMapping("/revokeThirteenMould")
    public ApiResultDTO revokeThirteenMould(@RequestHeader("X-Token") String tokenId) {
        try {
            UserDTO userDTO = getUser(tokenId);
            thirteenMouldStartAsyncService.revokeThirteenMould(userDTO);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("作废失败", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }

    /**
     * 十三月工资流程审核
     * @param agencyVerifyVo
     * @return
     */
    @PostMapping("/doWorkFlow")
    public ApiResultDTO doWorkFlow(@RequestHeader("X-Token") String tokenid,
                                   @RequestBody ThirteenAgencyVerifyVO agencyVerifyVo) {
        try {
            UserDTO userDTO = getUser(tokenid);
            WfActionDTO<List<ThirteenAgencyVerify>> wfActionDTO = new WfActionDTO();
            wfActionDTO.setMenuguid(agencyVerifyVo.getMenuId());
            if(!StringUtils.isEmpty(agencyVerifyVo.getAuditExplain())) {
                wfActionDTO.setMessage(agencyVerifyVo.getAuditExplain());
            }else{
                wfActionDTO.setMessage("同意");
            }
            // 审核
            wfActionDTO.setActionType(ActionTypeEnum.WF_AUDIT.getActionTypeCode());
            wfActionDTO.setBizDatas(agencyVerifyVo.getAgencyVerifyList());
            wfActionDTO.setBizKeyName("bizKey");
            if(!StringUtils.isEmpty(agencyVerifyVo.getParams())&&agencyVerifyVo.getParams().indexOf(SystemDictConstant.WORK_FLOW_SUBMIT)!=-1) {
                for(ThirteenAgencyVerify tv:agencyVerifyVo.getAgencyVerifyList()){
                    tv.setIsSalarySubmit(SystemDictConstant.IS_TRUE);
                }
            }
            for(ThirteenAgencyVerify tv:agencyVerifyVo.getAgencyVerifyList()){
                tv.setAgencyInfoVerifiedCode(agencyVerifyVo.getMenuName()+"已审核");
            }
            iSalaryWorkFlowService.doSalaryFlowList(userDTO,wfActionDTO,agencyVerifyVo,tokenid);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("审核失败", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }

    /**
     * 十三月工资流程撤销审核
     * @param agencyVerifyVo
     * @return
     */
    @PostMapping("/rejectWorkFlow")
    public ApiResultDTO rejectWorkFlow(@RequestHeader("X-Token") String tokenid,
                                       @RequestBody ThirteenAgencyVerifyVO agencyVerifyVo) {
        try {
            UserDTO userDTO = getUser(tokenid);
            WfActionDTO<List<ThirteenAgencyVerify>> wfActionDTO = new WfActionDTO();
            wfActionDTO.setMenuguid(agencyVerifyVo.getMenuId());
            // 撤销
            wfActionDTO.setActionType(ActionTypeEnum.WF_RECALL.getActionTypeCode());
            wfActionDTO.setBizDatas(agencyVerifyVo.getAgencyVerifyList());
            wfActionDTO.setBizKeyName("bizKey");
            if(!StringUtils.isEmpty(agencyVerifyVo.getParams())&&agencyVerifyVo.getParams().indexOf(SystemDictConstant.WORK_FLOW_SUBMIT)!=-1) {
                for(ThirteenAgencyVerify tv:agencyVerifyVo.getAgencyVerifyList()){
                    tv.setIsSalarySubmit(SystemDictConstant.IS_FALSE);
                }
            }
            for(ThirteenAgencyVerify tv:agencyVerifyVo.getAgencyVerifyList()){
                tv.setAgencyInfoVerifiedCode(agencyVerifyVo.getMenuName()+"撤销送审");
            }
            iSalaryWorkFlowService.doSalaryFlowList(userDTO,wfActionDTO,agencyVerifyVo,tokenid);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("撤销失败", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }

    /**
     * 十三月工资流程退回审核
     * @param agencyVerifyVo
     * @return
     */
    @PostMapping("/backWorkFlow")
    public ApiResultDTO backWorkFlow(@RequestHeader("X-Token") String tokenid,
                                     @RequestBody ThirteenAgencyVerifyVO agencyVerifyVo) {
        try {
            UserDTO userDTO = getUser(tokenid);
            WfActionDTO<List<ThirteenAgencyVerify>> wfActionDTO = new WfActionDTO();
            wfActionDTO.setMenuguid(agencyVerifyVo.getMenuId());
            // 退回
            wfActionDTO.setActionType(ActionTypeEnum.WF_BACK.getActionTypeCode());
            wfActionDTO.setMessage(agencyVerifyVo.getAuditExplain());
            wfActionDTO.setBizDatas(agencyVerifyVo.getAgencyVerifyList());
            wfActionDTO.setBizKeyName("bizKey");
            if(!StringUtils.isEmpty(agencyVerifyVo.getParams())&&agencyVerifyVo.getParams().indexOf(SystemDictConstant.WORK_FLOW_REPORT)!=-1) {
                for(ThirteenAgencyVerify tv:agencyVerifyVo.getAgencyVerifyList()){
                    tv.setIsSalarySubmit(SystemDictConstant.IS_FALSE);
                }
            }
            for(ThirteenAgencyVerify tv:agencyVerifyVo.getAgencyVerifyList()){
                tv.setAgencyInfoVerifiedCode(agencyVerifyVo.getMenuName()+"已退回");
            }
            iSalaryWorkFlowService.doSalaryFlowList(userDTO,wfActionDTO,agencyVerifyVo,tokenid);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("退回失败", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }

}
